<?php

namespace Modules\DataAnalysis\Http\Controllers\Admin;

use App\Http\Controllers\Admin\ApiController;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\DataAnalysis\Entities\Setting;
use Modules\DataAnalysis\Http\Requests\Admin\SystemSettingRequest;

class SystemSettingController extends ApiController
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index()
    {
        $data = Setting::query()->get();
        $list = collect([]);
        foreach ($data as $value) {
            $list[$value->key] = $value;
        }
        $data_lists['spider_setting'] = $list->where('type', 1)->toArray();
        $data_lists['nlp_setting'] = $list->where('type', 2)->toArray();

        return $this->success($data_lists);
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(SystemSettingRequest $request)
    {
        if ($request->get('type') == 1) {
            $data = $request->only(['collect_user_info', 'collect_comment_reply']);
        } else if ($request->get('type') == 2) {
            $data = $request->only(['score_line', 'edit_comment_score']);
        }

        foreach ($data as $key => $value) {
            Setting::query()->where('key', $key)->update(['value' => $value]);
        }
        return $this->success($data);
    }
}
